// Package middleware
// @File: requestID.go
// @Description:
// @Author: YaoRuiQi
// @Date: 2025/8/25 09:37

package middleware

import (
	"GinStandardTemplate/internal/pkg/contextUtils"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
)

// RequestIDKey requestID 唯一请求ID
const RequestIDKey = "X-Request-ID"

// RequestIDMiddleware
//
//	@Description: 请求ID中间件
//	@return gin.HandlerFunc
func RequestIDMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		requestID := uuid.New().String()

		// 创建新的请求上下文，注入requestID
		c.Request = c.Request.WithContext(contextUtils.NewRequestContext(
			c.Request.Context(), &contextUtils.RequestContext{
				RequestID: requestID,
			},
		))

		// 设置响应头
		c.Writer.Header().Set(RequestIDKey, requestID)

		c.Next()
	}
}
